﻿using System.Configuration;

namespace K3R.Web.Routing {
	public class ConfigRouteElement : ConfigurationElement {
		[ConfigurationProperty("virtualPath", IsRequired = false)]
		public string VirtualPath {
			get {
				return (string)base["virtualPath"];
			}
			set {
				base["virtualPath"] = value;
			}
		}

		[ConfigurationProperty("url", IsRequired = true, IsKey = true)]
		public string Url {
			get {
				return (string)base["url"];
			}
			set {
				base["url"] = value;
			}
		}

		[ConfigurationProperty("handler", IsRequired = false)]
		public string Handler {
			get {
				return (string)base["handler"];
			}
			set {
				base["handler"] = value;
			}
		}

		[ConfigurationProperty("ignore", DefaultValue = false)]
		public bool Ignore {
			get {
				return (bool)base["ignore"];
			}
			set {
				base["ignore"] = value;
			}
		}
	}

	public class ConfigRouteCollection : ConfigurationElementCollection {

		protected override ConfigurationElement CreateNewElement() {
			return new ConfigRouteElement();
		}

		protected override object GetElementKey(ConfigurationElement element) {
			return (element as ConfigRouteElement).Url;
		}

		public ConfigRouteElement this[int index] {
			get {
				return (ConfigRouteElement)BaseGet(index);
			}
			set {
				if (BaseGet(index) != null) {
					BaseRemoveAt(index);
				}
				BaseAdd(index, value);
			}
		}

		public new ConfigRouteElement this[string url] {
			get {
				return (ConfigRouteElement)BaseGet(url);
			}
		}

		public int IndexOf(ConfigRouteElement route) {
			return BaseIndexOf(route);
		}

		public new int Count {
			get { return base.Count; }
		}
	}

	public class RouteConfig : ConfigurationSection {
		[ConfigurationProperty("virtualPath", IsRequired = false)]
		public string VirtualPath {
			get {
				return (string)base["virtualPath"];
			}
			set {
				base["virtualPath"] = value;
			}
		}

		[ConfigurationProperty("handler", DefaultValue = "K3R.Web.Routing.RoutingHandler, K3R.Web.Routing")]
		public string Handler {
			get {
				return (string)base["handler"];
			}
			set {
				base["handler"] = value;
			}
		}

		[ConfigurationProperty("routeExisting", DefaultValue = false)]
		public bool RouteExisting {
			get {
				return (bool)base["routeExisting"];
			}
			set {
				base["routeExisting"] = value;
			}
		}

		[ConfigurationProperty("ignoreFavicon", DefaultValue = true)]
		public bool IgnoreFavicon {
			get {
				return (bool)base["ignoreFavicon"];
			}
			set {
				base["ignoreFavicon"] = value;
			}
		}

		[ConfigurationProperty("ignoreAxd", DefaultValue = true)]
		public bool IgnoreAxd {
			get {
				return (bool)base["ignoreAxd"];
			}
			set {
				base["ignoreAxd"] = value;
			}
		}

		[ConfigurationProperty("ignoreAsmx", DefaultValue = true)]
		public bool IgnoreAsmx {
			get {
				return (bool)base["ignoreAsmx"];
			}
			set {
				base["ignoreAsmx"] = value;
			}
		}

		[ConfigurationProperty("routes", IsDefaultCollection = true)]
		[ConfigurationCollection(typeof(ConfigRouteCollection), AddItemName="route")]
		public ConfigRouteCollection Routes {
			get {
				return (ConfigRouteCollection)base["routes"];
			}
		}
	}
}
